/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.bsu.android.gtv.apps.bconnected.data;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;

import edu.bsu.android.gtv.apps.bconnected.objects.Degree;
import edu.bsu.android.gtv.apps.bconnected.parser.ParserFactory;
import edu.bsu.android.gtv.apps.bconnected.parser.ServiceParser;
import edu.bsu.android.gtv.apps.bconnected.util.Constants;

/**
 * Data layer for the {@link Degree} object
 * @author Kyle Parker, University Computing Services
 */
public class DegreeData {
    private static ArrayList<Degree> mDegrees;

    /**
     * Loads an ArrayList<Degree> from the service handler containing the list of degrees based on the student's BSU ID
     * This is the "schedule" list for the student during a given term
     * @param bsuID
     * @param loginGuid - login guid used to determine whether the user is authorized to retrieve the data from the service handler
     * @param activity - used by the {@link ParserFactory} to retrieve the demo_account shared preference - this will determine what
     * 					 data source to use for the service handler.
     * @throws UnsupportedEncodingException - the encrypted BSU ID has to be encoded in order to pass as a querystring parameter
     */
    public static void initializeDegree(String bsuID, String loginGuid, Activity activity) 
    		throws UnsupportedEncodingException {
    	List<String> querystringParms = new ArrayList<String>(3);
    	querystringParms.add(Constants.QUERYSTRING_BSU_ID + URLEncoder.encode(bsuID, "UTF-8"));
		querystringParms.add(Constants.QUERYSTRING_LOGIN_GUID + loginGuid);

		ServiceParser parser = ParserFactory.getParser(Constants.SERVICE_TYPE_DEGREE, querystringParms, activity);
		mDegrees = parser.parseDegree();
    }

    /**
     * Returns a boolean indicating whether or not the student was authenticated to retrieve the degree data from the service handler. 
     * If the BSU ID and login guid passed to the service handler do not match the values in the SQL Server backend database, then the XML 
     * will contain an error indicator in the first element. 
     * @return true|false
     */
    public static boolean getDegreeAuth() {
    	if (mDegrees.size() > 0) {
    		if (mDegrees.get(0).getError() != null) {
    			return false;
    		}
    	}
    	
    	return true;
    }

    /**
     * Returns the integer value of the {@link Degree} object size
     * @return mDegrees.size() | 0
     */
    public static int getDegreeCount() {
    	if (mDegrees == null) {
    		return 0;
    	} else {
            return mDegrees.size();
    	}
    }

    /**
     * Returns a {@link Degree} object at the index specified
     * @param i - the index of the object to be returned
     * @return mDegrees.get(i)
     */
    public static Degree getDegree(int i) {
        return mDegrees.get(i);
    }

    /**
     * Returns the integer value of the {@link Degree} object size
     * @return mDegrees
     */
    public static ArrayList<Degree> getDegrees() {
        return mDegrees;
    }
}
